package fashionshop.bll;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;

import fashionshop.dal.OrderDAL;
import fashionshop.dal.OrderDetailDAL;

public class OrderDetailBLL {

	private int productID;
	private int orderID;
	private BigDecimal price;
	private int quantity;
	private BigDecimal sum;
	
	public OrderDetailBLL() {
	}
	
	public int getProductID() {
		return productID;
	}
	public void setProductID(int productID) {
		this.productID = productID;
	}
	
	public int getOrderID() {
		return orderID;
	}

	public void setOrderID(int orderID) {
		this.orderID = orderID;
	}

	public BigDecimal getPrice() {
		return price;
	}
	public void setPrice(BigDecimal price) {
		this.price = price;
	}
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public BigDecimal getSum() {
		return sum;
	}

	public void setSum(BigDecimal sum) {
		this.sum = sum;
	}
	
	public Vector loadOrderDetail(int orderID) throws Exception {
		
		Vector orderDetailTableRecords = new Vector();
		ResultSet result = OrderDetailDAL.LoadByOrderID(orderID);
		while (result.next()){
			Vector record = new Vector();
			record.add(result.getString("OrderID"));
			record.add(result.getString("ProductID"));
            record.add(result.getString("Code"));
            record.add(result.getString("Name"));
            record.add(result.getInt("Quantity"));
            record.add(result.getString("Price"));
            record.add(result.getString("Sum"));
			
            orderDetailTableRecords.add(record);
		}
		return orderDetailTableRecords;
	}
	
	public boolean SaveAll(int orderID, Vector orderDetailTableRecords) throws Exception {
		
		OrderDetailDAL.DeleteAll(orderID);
		boolean flag = true;
		
		for(int i = 0 ; i < orderDetailTableRecords.size(); i++){
			Vector vt = (Vector) orderDetailTableRecords.get(i);
			OrderDetailBLL orderDetail = new OrderDetailBLL();
			orderDetail.setOrderID(orderID);
			orderDetail.setProductID(Integer.parseInt(vt.get(1).toString()));
			
			int quantity = Integer.parseInt(vt.get(4).toString());
			orderDetail.setQuantity(quantity);
			
			BigDecimal price;
			if(vt.get(5) != null )
				price = new BigDecimal(vt.get(5).toString());
			else
				price = new BigDecimal("0");
			orderDetail.setPrice(price);
			
			orderDetail.setSum(price.multiply(BigDecimal.valueOf(quantity)));
			
			if(OrderDetailDAL.Insert(orderDetail) == 0){
				flag = false;
				break;
			}
		}
			
		return flag;
	}
}
